package cn.xinfei.xdecision.common.service.component.decisiontable.impl;

import cn.xinfei.xdecision.common.dao.component.decisiontable.DecisionTablesDetailConditionMapper;
import cn.xinfei.xdecision.common.model.component.decisiontable.DecisionTablesDetailCondition;
import cn.xinfei.xdecision.common.service.component.decisiontable.DecisionTablesDetailConditionService;
import cn.xinfei.xdecision.common.utils.util.strategyx.CustomValueUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * (DecisionTablesDetailCondition)表服务实现类
 */
@Service("decisionTablesDetailConditionService")
@Slf4j
public class DecisionTablesDetailConditionServiceImpl extends ServiceImpl<DecisionTablesDetailConditionMapper, DecisionTablesDetailCondition> implements DecisionTablesDetailConditionService {


//    @Override
//    public List<DecisionTablesDetailCondition> queryByDecisionTablesDetailId(Long detailId) {
//        if (detailId==null){
//            return null;
//        }
//        DecisionTablesDetailCondition query = new DecisionTablesDetailCondition();
//        query.setDetailId(detailId);
//        List<DecisionTablesDetailCondition> list = this.list(new QueryWrapper<>(query));
//        return list;
//    }

    @Override
    public List<DecisionTablesDetailCondition> queryByDecisionTablesDetailId(Long detailId) {
        if (detailId == null) {
            return null;
        }

        DecisionTablesDetailCondition query = new DecisionTablesDetailCondition();
        query.setDetailId(detailId);
        List<DecisionTablesDetailCondition> list = this.list(new QueryWrapper<>(query));

        return list;
    }

    @Override
    public List<DecisionTablesDetailCondition> updateDecisionTablesDetailCondition(Long detailId, List<DecisionTablesDetailCondition> list) {
        return null;
    }

    @Override
    public List<DecisionTablesDetailCondition> insertDecisionTablesDetailCondition(Long detailId, List<DecisionTablesDetailCondition> list) {
        if (detailId == null || list == null || list.isEmpty()) {
            return null;
        }
        for (DecisionTablesDetailCondition condition : list) {
            condition.setDetailId(detailId);
        }
        boolean b = this.saveBatch(list);
        if (b) {
            return this.queryByDecisionTablesDetailId(detailId);
        }
        return null;
    }

    @Override
    public boolean deleteByDecisionTablesDetailId(List<Long> detailIds) {
        LambdaUpdateWrapper<DecisionTablesDetailCondition> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(DecisionTablesDetailCondition::getDetailId, detailIds);
        return this.remove(wrapper);
//        int result = decisionTablesDetailConditionMapper.deleteByDetailIds(detailIds);
//        return result>0?true:false;
    }

    @Override
    public List<String> queryFieldEnByDetailIds(List<Long> detailIds) {
        if (detailIds == null || detailIds.isEmpty()) {
            return null;
        }
        List<DecisionTablesDetailCondition> list = new ArrayList<>();
        Set<String> fieldEns = new HashSet<>();

        LambdaQueryWrapper<DecisionTablesDetailCondition> conditionWrapper = new LambdaQueryWrapper<>();
        conditionWrapper.in(DecisionTablesDetailCondition::getDetailId, detailIds);
        list = this.list(conditionWrapper);
        for (DecisionTablesDetailCondition condition : list) {
            if (condition.getValueType() == null || condition.getValueType() == 1) {
                continue;
            }
            if (condition.getValueType() == 2) {
                fieldEns.add(condition.getValue());
            } else if (condition.getValueType() == 3) {
                fieldEns.addAll(CustomValueUtils.getFieldEnSet(condition.getValue()));
            }
        }
        return new ArrayList<>(fieldEns);
    }
}
